#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   NRC22_Phy_test.py
@Time    :   2022/09/02 13:31:28
@Author  :   YangLiang
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pickle import TRUE
from pytest_assume.plugin import assume

import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True


@allure.severity('critical')
class Test_SID0x31_NRC22_Phy(object):

     @allure.story('ECU NRC 22 Test')
     @allure.title('Test 0x31 Service Undervoltage condition ')
     @allure.description('Test 0x31 Service Undervoltage confition response NRC22 in extended session with physical addressing')
     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SID31_UnderVoltage_Extended_Phy(self, action, Driver):
          iStep = 1
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 3, iStep, False)
          iStep = Driver['CAN'].SetVolt(Driver, 6, 1, iStep)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 0, iStep, False)
          time.sleep(1)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 0, iStep, False)
          time.sleep(1)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31010203', '7F3122', 0, iStep, False)

          iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31010203', '71', 0, iStep, False)

     @allure.title('Test 0x31 Service OverVoltage condition ')
     @allure.description('Test 0x31 Service OverVoltage confition response NRC22 in extended session with physical addressing')
     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SID31_OverVoltage_Extended_Phy(self, action, Driver):
          iStep = 1
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 3, iStep, False)
          iStep = Driver['CAN'].SetVolt(Driver, 18.5, 1, iStep)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 0, iStep, False)
          time.sleep(1)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00', 0, iStep, False)
          time.sleep(1)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31010203', '7F3122', 0, iStep, False)
          iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31010203', '71', 0, iStep, False)

     @allure.title('Test 0x31 Service Vspeed over 3kph condition ')
     @allure.description('Test 0x31 Service Vspeed over 3kph confition response NRC22 in extended session with physical addressing')
     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SID31_OverVspeed_Extended_Phy(self, action, Driver):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E, 0x00], [0x7E, 0x00], 0, iStep, False)

          #设置车速大于3KM/H
          iStep = Driver['CAN'].setSignalValue('abs_sts_run1', 'abs_pm_veh_spd',80,iStep)
          
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x31,0x01,0x02,0x03], [0x7F,0x31,0x22], 0, iStep, False)

          #设置车速为0KM/H
          iStep = Driver['CAN'].setSignalValue('abs_sts_run1', 'abs_pm_veh_spd',0,iStep)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31010203', '71', 0, iStep, False)

     '''3E 80'''
     @allure.story('ECU NRC 22 Test')
     @allure.title('Test 0x31 Service Undervoltage condition ')
     @allure.description('Test 0x31 Service Undervoltage confition response NRC22 in extended session with physical addressing')
     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SID31_UnderVoltage_Extended_Send3E80_Phy(self, action, Driver):
          iStep = 1
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 3, iStep, False)
          iStep = Driver['CAN'].SetVolt(Driver, 6, 1, iStep)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 0, iStep, False)
          time.sleep(1)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 0, iStep, False)
          time.sleep(1)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31010203', '7F3122', 0, iStep, False)

          iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31010203', '71', 0, iStep, False)

     @allure.title('Test 0x31 Service OverVoltage condition ')
     @allure.description('Test 0x31 Service OverVoltage confition response NRC22 in extended session with physical addressing')
     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SID31_OverVoltage_Extended_Send3E80_Phy(self, action, Driver):
          iStep = 1
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 3, iStep, False)
          iStep = Driver['CAN'].SetVolt(Driver, 18.5, 1, iStep)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 0, iStep, False)
          time.sleep(1)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E80', '', 0, iStep, False)
          time.sleep(1)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31010203', '7F3122', 0, iStep, False)
          iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31010203', '71', 0, iStep, False)


     @allure.title('Test 0x31 Service Vspeed over 3kph condition ')
     @allure.description('Test 0x31 Service Vspeed over 3kph confition response NRC22 in extended session with physical addressing')
     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_SID31_OverVspeed_Extended_Send3E80_Phy(self, action, Driver):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x10, 0x03], [0x50, 0x03, 0x00, 0x32, 0x01, 0xF4], 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x3E, 0x80], [], 0, iStep, False)

          #设置车速大于3KM/H
          iStep = Driver['CAN'].setSignalValue('abs_sts_run1', 'abs_pm_veh_spd',80,iStep)
          
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x31,0x01,0x02,0x03], [0x7F,0x31,0x22], 0, iStep, False)

          #设置车速为0KM/H
          iStep = Driver['CAN'].setSignalValue('abs_sts_run1', 'abs_pm_veh_spd',0,iStep)
          iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31010203', '71', 0, iStep, False)


